using System;
using Slaks.DocumentSerialization.Document;
using Slaks.DocumentSerialization.Document.Wordnet;
using Slaks.Graphs.Wordnet;

namespace Slaks.Graphs.GraphBuilder.Wordnet
{
	/// <summary>
	/// Summary description for StandardRepresentationSlaks.
	/// </summary>
	public class WordnetSynStandardRepresentation : WordnetSynGraphBuilder, IWordnetSynRepresentation
	{
		#region IWordnetSynRepresentation Members

		public WordnetGraph Build(WordnetDocument document, int numberOfVerticesToLeave)
		{
			string documentName = document.FileName;
			int index = documentName.LastIndexOfAny(new char[] {'\\', '/'});
			if (index >= 0 && (index + 1 < documentName.Length)) documentName = documentName.Substring(index + 1);

			WordnetGraph graph = new WordnetGraph(documentName);
			WordnetGraph tempGraph = this.BuildTemporalGraph(document);
			this.RemoveInfrequentVerticesInTemporalGraph(tempGraph, numberOfVerticesToLeave);

			WordnetVertex vertex;
			string sectionType;

			//iterate through nodes
			foreach (WordnetNode node in document.Nodes)
			{
				//check if the vertex is in the list of frequent nodes.
				if (tempGraph.GetVertex(node.Id) == null) continue;
				vertex = graph.CreateAddVertex(node.Id);
				vertex.Frequency = node.TotalTermFrequency; // * this.GetSenseProbability(node);

				CopySynonyms(node, vertex);

				//iterate through sections
				foreach (Section section in node.Sections)
				{
					sectionType = section.GetSectionTypeString();

					Section.SectionType sectionTp = Section.GetSectionTypeByString(sectionType);

					//iterate through sentences
					foreach (Sentence sentence in section.Sentences)
					{
						AddRelations(document, graph, tempGraph, vertex, 1, 1, sentence, sectionTp, BuilderType.WordnetSynStandardRepresentation);
					}
				}
			}


			return graph;
		}

		public WordnetGraph Build(WordnetDocument document, int numberOfVerticesToLeave, int nParameter)
		{
			throw new NotImplementedException("WordnetSynStandardRepresentation does not implement this method");
		}

		#endregion
	}
}